<template>
	<div class="popoer">
		<div class="reference" @click="showMenu($event)">
			<slot name="reference"></slot>
		</div>
		<div class="menu-body" :class="[placement]" :style="{width:width + 'rpx'}">
			<div class="menu" v-show="isShowMenu" :class="[theme,placement]">
				<div @click="closeAllPopover">
					<slot v-show="isShowMenu"></slot>
				</div>
				<div class="menu-item" v-for="(item,index) in actions" :key="index" :class="{'disabled':item.disabled}" @click="onSelect(item)">
					<span>{{item.text}}</span>
				</div>
			</div>
		</div>
		<div v-if="isShowMenu" @click="closeAllPopover" class="mask"></div>
	</div>
</template>

<script>
	export default {
		props: {
			theme: {
				type: String,
				default: ''
			},
			actions: {
				type: Array,
				default: []
			},
			placement: {
				type: String,
				default: 'bottom'
			},
			width: {
				type: String,
				default: 'auto'
			}
		},
		data() {
			return {
				isShowMenu: false
			}
		},
		methods: {
			closeAllPopover() {
				this.$parent.$children.forEach(item => {
					item.isShowMenu = false;
				})
			},
			onSelect(item) {
				if (!item.disabled) {
					this.closeAllPopover();
					this.$emit('select', item);
					this.isShowMenu = false;
				}
			},
			showMenu(e) {
				if (!this.isShowMenu) this.closeAllPopover();
				this.isShowMenu = !this.isShowMenu;
			}
		}
	}
</script>

<style>
	.popoer {
		position: relative;
		display: inline-block;
	}
	.popoer > div {
		z-index: 999;
	}
	.popoer .menu-body {
		position: absolute;
		z-index: 10;
	}

	.popoer .menu-body.bottom {
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
	}

	.popoer .menu-body.bottom-start {
		bottom: 0;
		left: 0;
	}

	.popoer .menu-body.bottom-end {
		bottom: 0;
		right: 0;
	}

	.popoer .menu-body.top,
	.popoer .menu-body.top-start,
	.popoer .menu-body.top-end {
		top: 0;
	}

	.popoer .menu-body.top {
		left: 50%;
		transform: translateX(-50%);
	}

	.popoer .menu-body.top-end {
		right: 0;
	}

	.popoer .menu {
		position: absolute;
		background: #ccc;
		right: 16rpx;
		border-radius: 5px;
		color: #515a6e;
		box-shadow: 0upx 0upx 30upx rgba(0, 0, 0, 0.2);
		background: #fff;
		padding: 10rpx 0;
	}

	.popoer .menu::after {
		content: "";
		position: absolute;
		border-width: 0 20rpx 20rpx;
		border-style: solid;
		border-color: transparent transparent #fff;
	}

	.popoer .menu.dark {
		background: #4A4A4A;
		color: #fff;
	}

	.popoer .menu.dark::after {
		border-color: transparent transparent #4A4A4A;
	}

	.popoer .menu.bottom {
		top: 22rpx;
		left: 0;
		right: 0;
		margin: auto;
	}

	.popoer .menu.bottom-start {
		top: 22rpx;
		left: 0;
	}

	.popoer .menu.bottom-end {
		top: 22rpx;
		right: 0;
	}

	.popoer .menu.bottom::after {
		top: -18rpx;
		right: 10rpx;
		right: 50%;
		transform: translateX(50%);
	}

	.popoer .menu.bottom-start::after {
		top: -18rpx;
		right: 10rpx;
		right: initial;
		left: 10px;
	}

	.popoer .menu.bottom-end::after {
		top: -18rpx;
		right: 10rpx;
		left: initial;
		right: 10px;
	}

	.popoer .menu.top,
	.popoer .menu.top-start,
	.popoer .menu.top-end {
		bottom: 22rpx;
	}

	.popoer .menu.top {
		left: 0;
		right: 0;
		margin: auto;
	}

	.popoer .menu.top-start {
		left: 0;
	}

	.popoer .menu.top-end {
		right: 0;
	}

	.popoer .menu.top::after,
	.popoer .menu.top-start::after,
	.popoer .menu.top-end::after {
		border-width: 20rpx 20rpx 0;
		border-style: solid;
		border-color: #fff transparent transparent;
		bottom: -18rpx;
	}

	.popoer .menu.top::after {
		right: 50%;
		transform: translateX(50%);
	}

	.popoer .menu.top-start::after {
		left: 0;
	}

	.popoer .menu.top-end::after {
		right: 0;
	}

	.popoer .menu-item {
		border-bottom: 1px solid #eee;
		padding: 20rpx;
		white-space: nowrap;
	}

	.popoer .menu-item:last-child {
		border: none;
	}

	.popoer .menu-item.disabled {
		color: #c8c9cc;
		cursor: not-allowed;
	}

	.popoer .menu.dark .menu-item.disabled {
		color: #9fa0a2;
	}
	
	.popoer > div.mask{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
	}
	
</style>
